#!/usr/bin/perl
my $colourlightgreen = "00CC00";
my $colourlightred = "FF3300";
my $pallino_verde = "<img src='images/pallino_verde.gif' width='14' height='14' border='0' title='Running'>";
my $pallino_rosso = "<img src='images/pallino_rosso.gif' width='14' height='14' border='0' title='Stopped'>";
my $pallino_arancione = "<img src='images/pallino_arancione.gif' width='14' height='14' border='0' title='Offline'>";
my $pallino_arancione_mdstat = "<img src='images/pallino_arancione.gif' width='14' height='14' border='0' title='ReSync'>";
my $table1colour = "FFFFFF";
my $table2colour = "66CCFF";
my $flashpanel = "pidof -x op_server.pl";
my $snmpd = "pidof -x snmpd";
my $tftp = "netstat -anp | grep -i 0.0.0.0:69";
my $ntpd = "pidof -x ntpd";
my $mysqld = "pidof -x mysqld";
my $vsftpd = "pidof -x vsftpd";
my $argusd = "pidof -x argusd";
my $webmin = "ps -A | grep -i miniserv.pl";
my $drbd = "cat /proc/drbd | grep -i UpToDate/UpToDate";
my $heartbeat = "pidof -x heartbeat";
my $mysqlcluster = "/usr/local/sbin/mysqlclustercheck.sh";
my $clustercheck = "grep -e '0202' -e '0103' /etc/voiper_pn";
my $procheck = "grep -e '0102' /etc/voiper_pn";
my $mdstatresynch = "cat /proc/mdstat | grep -i resync";
my $mdstatonline = "cat /proc/mdstat | grep -i UU";
my $bi4sftpd = "pidof -s bi4sftp";

# Maps a nice printable name to the changing part of the pid file, which is also the name of the program.
my %servicenames = (
	'+ asterisk core' => 'asterisk/asterisk',
	'+ web server' => 'httpd',
	'+ cron server' => 'crond',
	'+ sshd server' => 'sshd',
	'+ ddns client' => 'ddclient',
	'+ dhcp server' => 'dhcpd'
);

print "<TABLE WIDTH='180' CELLPADDING='2' CELLSPACING='1'>";

my $lines = 0;
my $key = '';

foreach $key (keys %servicenames) {
        print "<TR>";
        print "<TD WIDTH='80%'><font color='#000000' size='2' face='Verdana, Arial, Helvetica, sans-serif'>$key</font></TD>";
        my $shortname = $servicenames{$key};
        my $status = &isrunning($shortname);
        print "<TD WIDTH='20%'><div align='right'>$status</div></font></TD>";
        print "</TR>";
        $lines++;
}

if (open(FILE, "/etc/cron.daily/00-logwatch")) {
    print "<TR><TD WIDTH='80%'><font color='#000000' size='2' face='Verdana, Arial, Helvetica, sans-serif'>+ logwatch cron</font></TD>";
    print "<TD WIDTH='20%'><div align='right'>$pallino_verde</div></TD></TR>";
    } else {
        print "<TR><TD WIDTH='80%'><font color='#000000' size='2' face='Verdana, Arial, Helvetica, sans-serif'>+ logwatch cron</font></TD>";
		print "<TD WIDTH='20%'><div align='right'>$pallino_rosso</div></TD></TR>";
}

if (system("$snmpd &> /dev/null") == 0) {
    print "<TR><TD WIDTH='80%'><font color='#000000' size='2' face='Verdana, Arial, Helvetica, sans-serif'>+ snmpd server</font></TD>";
    print "<TD WIDTH='20%'><div align='right'>$pallino_verde</div></TD></TR>";
    } else {
		print "<TR><TD WIDTH='80%'><font color='#000000' size='2' face='Verdana, Arial, Helvetica, sans-serif'>+ snmpd server</font></TD>";
		print "<TD WIDTH='20%'><div align='right'>$pallino_rosso</div></TD></TR>";
}

if (system("$tftp &> /dev/null") == 0) {
	print "<TR><TD WIDTH='80%'><font color='#000000' size='2' face='Verdana, Arial, Helvetica, sans-serif'>+ tftpd server</font></TD>";
	print "<TD WIDTH='20%'><div align='right'>$pallino_verde</div></TD></TR>";
    } else {
        print "<TR><TD WIDTH='80%'><font color='#000000' size='2' face='Verdana, Arial, Helvetica, sans-serif'>+ tftpd server</font></TD>";
		print "<TD WIDTH='20%'><div align='right'>$pallino_rosso</div></TD></TR>";
}

if (system("$ntpd &> /dev/null") == 0) {
	print "<TR><TD WIDTH='80%'><font color='#000000' size='2' face='Verdana, Arial, Helvetica, sans-serif'>+ ntpd server</font></TD>";
	print "<TD WIDTH='20%'><div align='right'>$pallino_verde</div></TD></TR>";
    } else {
        print "<TR><TD WIDTH='80%'><font color='#000000' size='2' face='Verdana, Arial, Helvetica, sans-serif'>+ ntpd server</font></TD>";
	    print "<TD WIDTH='20%'><div align='right'>$pallino_rosso</div></TD></TR>";
}

if (system("$mysqld &> /dev/null") == 0) {
    print "<TR><TD WIDTH='80%'><font color='#000000' size='2' face='Verdana, Arial, Helvetica, sans-serif'>+ mysqld server</font></TD>";
	print "<TD WIDTH='20%'><div align='right'>$pallino_verde</div></TD></TR>";
	} else {
        print "<TR><TD WIDTH='80%'><font color='#000000' size='2' face='Verdana, Arial, Helvetica, sans-serif'>+ mysqld server</font></TD>";
		print "<TD WIDTH='20%'><div align='right'>$pallino_rosso</div></TD></TR>";
}

if (system("$vsftpd &> /dev/null") == 0) {
	print "<TR><TD WIDTH='80%'><font color='#000000' size='2' face='Verdana, Arial, Helvetica, sans-serif'>+ vsftpd server</font></TD>";
	print "<TD WIDTH='20%'><div align='right'>$pallino_verde</div></TD></TR>";
	} else {
		print "<TR><TD WIDTH='80%'><font color='#000000' size='2' face='Verdana, Arial, Helvetica, sans-serif'>+ vsftpd server</font></TD>";
    	print "<TD WIDTH='20%'><div align='right'>$pallino_rosso</div></TD></TR>";
}

if (system("$argusd &> /dev/null") == 0) {
	print "<TR><TD WIDTH='80%'><font color='#000000' size='2' face='Verdana, Arial, Helvetica, sans-serif'>+ argusd server</font></TD>";
	print "<TD WIDTH='20%'><div align='right'>$pallino_verde</div></TD></TR>";
	} else {
		print "<TR><TD WIDTH='80%'><font color='#000000' size='2' face='Verdana, Arial, Helvetica, sans-serif'>+ argusd server</font></TD>";
		print "<TD WIDTH='20%'><div align='right'>$pallino_rosso</div></TD></TR>";
}

if (system("$webmin &> /dev/null") == 0) {
	print "<TR><TD WIDTH='80%'><font color='#000000' size='2' face='Verdana, Arial, Helvetica, sans-serif'>+ webmin server</font></TD>";
	print "<TD WIDTH='20%'><div align='right'>$pallino_verde</div></TD></TR>";
	} else {
		print "<TR><TD WIDTH='80%'><font color='#000000' size='2' face='Verdana, Arial, Helvetica, sans-serif'>+ webmin server</font></TD>";
		print "<TD WIDTH='20%'><div align='right'>$pallino_rosso</div></TD></TR>";
}

if (system("$flashpanel &> /dev/null") == 0) {
    print "<TR><TD WIDTH='80%'><font color='#000000' size='2' face='Verdana, Arial, Helvetica, sans-serif'>+ flash panel</font></TD>";
    print "<TD WIDTH='20%'><div align='right'>$pallino_verde</div></TD></TR>";
    } else {
        print "<TR><TD WIDTH='80%'><font color='#000000' size='2' face='Verdana, Arial, Helvetica, sans-serif'>+ flash panel</font></TD>";
        print "<TD WIDTH='20%'><div align='right'>$pallino_rosso</div></TD></TR>";
}

if (system("$procheck &> /dev/null") == 0) {
    if (system("$mdstatresynch &> /dev/null") == 0) {
        print "<TR><TD WIDTH='80%'><font color='#000000' size='2' face='Verdana, Arial, Helvetica, sans-serif'>+ software raid1</font></TD>";
	    print "<TD WIDTH='20%'><div align='right'>$pallino_arancione_mdstat</div></TD></TR>";
    } elsif (system("$mdstatonline &> /dev/null") == 0) {
            print "<TR><TD WIDTH='80%'><font color='#000000' size='2' face='Verdana, Arial, Helvetica, sans-serif'>+ software raid1</font></TD>";
            print "<TD WIDTH='20%'><div align='right'>$pallino_verde</div></TD></TR>";
    } else {
                print "<TR><TD WIDTH='80%'><font color='#000000' size='2' face='Verdana, Arial, Helvetica, sans-serif'>+ software raid1</font></TD>";
			    print "<TD WIDTH='20%'><div align='right'>$pallino_rosso</div></TD></TR>";
    }
}

if (open(FILE, "/etc/cron.weekly/vagent")) {
    print "<TR><TD WIDTH='80%'><font color='#000000' size='2' face='Verdana, Arial, Helvetica, sans-serif'>+ voiper agent</font></TD>";
    print "<TD WIDTH='20%'><div align='right'>$pallino_verde</div></TD></TR>";
    } else {
        print "<TR><TD WIDTH='80%'><font color='#000000' size='2' face='Verdana, Arial, Helvetica, sans-serif'>+ voiper agent</font></TD>";
		print "<TD WIDTH='20%'><div align='right'>$pallino_rosso</div></TD></TR>";
}

if (system("$bi4sftpd &> /dev/null") == 0) {
	print "<TR><TD WIDTH='80%'><font color='#000000' size='2' face='Verdana, Arial, Helvetica, sans-serif'>+ bi4data client</font></TD>";
	print "<TD WIDTH='20%'><div align='right'>$pallino_verde</div></TD></TR>";
	} else {
		print "<TR><TD WIDTH='80%'><font color='#000000' size='2' face='Verdana, Arial, Helvetica, sans-serif'>+ bi4data client</font></TD>";
		print "<TD WIDTH='20%'><div align='right'>$pallino_rosso</div></TD></TR>";
}

if (system("$clustercheck &> /dev/null") == 0) {
    if (system("$drbd &> /dev/null") == 0) {
        print "<TR><TD WIDTH='80%'><font color='#000000' size='2' face='Verdana, Arial, Helvetica, sans-serif'>+ drbd cluster</font></TD>";
        print "<TD WIDTH='20%'><div align='right'>$pallino_verde</div></TD></TR>";
        } else {
            print "<TR><TD WIDTH='80%'><font color='#000000' size='2' face='Verdana, Arial, Helvetica, sans-serif'>+ drbd cluster</font></TD>";
			print "<TD WIDTH='20%'><div align='right'>$pallino_arancione</div></TD></TR>";
    }
    if (system("$mysqlcluster &> /dev/null") == 0) {  
        print "<TR><TD WIDTH='80%'><font color='#000000' size='2' face='Verdana, Arial, Helvetica, sans-serif'>+ mysql cluster</font></TD>";
		print "<TD WIDTH='20%'><div align='right'>$pallino_verde</div></TD></TR>";
        } else {
            print "<TR><TD WIDTH='80%'><font color='#000000' size='2' face='Verdana, Arial, Helvetica, sans-serif'>+ mysql cluster</font></TD>";
			print "<TD WIDTH='20%'><div align='right'>$pallino_arancione</div></TD></TR>";
    }
    if (system("$heartbeat &> /dev/null") == 0) {
        print "<TR><TD WIDTH='80%'><font color='#000000' size='2' face='Verdana, Arial, Helvetica, sans-serif'>+ heartbeat core</font></TD>";
        print "<TD WIDTH='20%'><div align='right'>$pallino_verde</div></TD></TR>";
        } else {
            print "<TR><TD WIDTH='80%'><font color='#000000' size='2' face='Verdana, Arial, Helvetica, sans-serif'>+ heartbeat core</font></TD>";
			print "<TD WIDTH='20%'><div align='right'>$pallino_rosso</div></TD></TR>";
    }
}

print "</TABLE>";

sub isrunning
{
        my $cmd = $_[0];
        my $status = "$pallino_rosso";
        my $pid = '';
        my $testcmd = '';
        my $exename;

        $cmd =~ /(^[a-z]+)/;
        $exename = $1;

        if (open(FILE, "/var/run/${cmd}.pid"))
        {
                $pid = <FILE>; chop $pid;
                close FILE;
                if (open(FILE, "/proc/${pid}/status"))
                {
                        while (<FILE>)
                        {
                                if (/^Name:\W+(.*)/) {
                                        $testcmd = $1; }
                        }
                        close FILE;
                        if ($testcmd =~ /$exename/)
                        {
                                $status = "$pallino_verde";
                                if (open(FILE, "/proc/${pid}/cmdline"))
                                {
                                        my $cmdline = <FILE>;
                                        if (!$cmdline) {
                                                $status = "$status (swapped)"; }
                                }
                        }
                }
        }

        return $status;
}
